<?php
namespace app\modules\web\controllers;

use app\common\service\UrlService;
use app\models\User;
use app\modules\web\controllers\common\BaseController;

class UserController extends BaseController
{
    public $layout = 'main';
    //登录页面
    public function actionLogin()
    {
        //如果是get请求，直接展示登录页面
        if( \Yii::$app->request->isGet ){
            $this->layout = 'user';
            return $this->render('login');
        }
        //登录逻辑处理
        $login_name = trim( $this->post( 'login_name' ) );
        $login_pwd  = trim( $this->post( 'login_pwd' ) );
        if( !$login_name || !$login_pwd ){
            $this->renderJs( '请重新填写用户名和密码-1',UrlService::buildWwwUrl( 'user/login' ) );
        }

        //从用户表 获取 $login_name = $login_name 信息是否存在
        $user_info = User::find()->where(['login_name' => $login_name])->one();

        if( !$user_info ){
            $this->renderJs( '请重新填写用户名和密码-1',UrlService::buildWwwUrl( 'user/login' ) );
        }

        //验证密码
        //密码加密算法 = md5( login_pwd + md5( $login_salt ) )
        $auth_pwd = md5( $login_pwd . md5( $user_info['login_salt'] ) );
        if( $auth_pwd != $user_info['login_pwd'] ){
            $this->renderJs( '请重新填写用户名和密码-1',UrlService::buildWwwUrl( 'user/login' ) );
        }


        // 保存用户的登录状态 cookies
        // 加密字符串 + '#' + uid , 加密字符串 = md5( login_name + login_pwd + login_salt )
      $this->setLoginStatus( $user_info );

        return $this->redirect( UrlService::buildWwwUrl('dashboard/index') );
    }

    //编辑当前登录人信息
    public function actionEdit()
    {
        if( \Yii::$app->request->isGet ){
            return $this->render('edit',['user_info'=>$this->current_user]);
        }

        $nickname = trim( $this->post( 'nickname' ) );
        $email = trim( $this->post( 'email' ) );
        if( mb_strlen( $nickname,'utf-8' ) < 1 ){
            return $this->renderJson([],"请输入合法的用户名~~","-1");
        }

        if( mb_strlen( $email,'utf-8' ) < 1 ){
            return $this->renderJson( [],"请输入合法的邮箱~~","-1" );
        }

        $user_info = $this->current_user;
        $user_info->nickname = $nickname;
        $user_info->email = $email;
        $user_info->updated_time = date("Y-m-d H:i:s");
        $user_info->update(0);
        return $this->renderJson( [],"编辑成功~~" );


    }
    //重置当前登录的密码
    public function actionResetPwd()
    {
        if( \Yii::$app->request->isGet ){
            $user_info = $this->current_user;
            return $this->render('reset_pwd',['user_info'=>$user_info]);
        }

        $old_password = trim( $this->post( "old_password","" ) );
        $new_password = trim( $this->post( "new_password","" ) );

        if( mb_strlen( $old_password,"utf-8" ) < 1 ){
            return $this->renderJson( [],"请输入原密码","-1" );
        }

        if( mb_strlen( $new_password,"utf-8" ) < 1 ){
            return $this->renderJson( [],"请输入新密码~~","-1" );
        }

        if( $old_password == $new_password ){
            return $this->renderJson( [],"新密码不能与老密码相同，请重新输入~~" );
        }

        //判断原密码是否正确
        $user_info = $this->current_user;
        if( !$user_info->verifyPassword( $old_password ) ){
            return $this->renderJson( [],"请检查原密码是否正确~~","-1" );
        }

        $user_info->setPassword( $new_password );
        $user_info->updated_time = date( "Y-m-d H:i:s" );
        $user_info->update(0);

        $this->setLoginStatus( $user_info );

        return $this->renderJson( [],"重置密码成功~~" );
    }
    //登出
    public function actionLogout()
    {
        $this->removeCookie( 'imooc_book' );
        return $this->redirect( UrlService::buildWwwUrl( 'user/login' ) );
    }
}